from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime

# 购物车创建请求模型（对应前端传入的数据）
class CartCreate(BaseModel):
    user_id: int = Field(..., description="用户ID")
    product_id: int = Field(..., description="商品ID")
    product_name: str = Field(..., max_length=255, description="商品名称")
    specs: str = Field(..., max_length=255, description="规格描述（如：尺寸:大; 甜度:半糖）")
    toppings: str = Field("", max_length=255, description="小料描述（如：珍珠; 椰果）")
    quantity: int = Field(1, ge=1, description="商品数量")
    price: float = Field(..., description="加入时的单价（含小料）")
    image_url: str = Field(..., max_length=255, description="商品图片URL")
    
    class Config:
        from_attributes = True  # 支持从ORM模型转换

# 购物车项返回模型（包含数据库自动生成的字段）
class CartItem(CartCreate):
    id: int = Field(..., description="购物车记录ID")
    add_time: datetime = Field(..., description="添加时间")
    update_time: datetime = Field(..., description="更新时间")
    is_deleted: bool = Field(False, description="逻辑删除标记")
    
    class Config:
        from_attributes = True  # 支持从ORM模型转换

# 购物车列表返回模型
class CartListResponse(BaseModel):
    items: List[CartItem] = Field(..., description="购物车项列表")
    total: int = Field(..., description="购物车商品总数")
    total_price: float = Field(..., description="购物车总价")
    #更新购物车请求模型
class ShopcarUpdate(BaseModel):
    
    quantity: int = Field(..., description="商品数量", ge=1)
    user_id: int = Field(..., description="用户ID")   



class CartItem(BaseModel):
    user_id: int
    product_id: int
    image_url: str
    product_name: str
    price: float
    quantity: int
    specs: str = ""
    toppings: str = ""

class CartListResponse(BaseModel):
    code: int = 200
    message: str = "success"
    data: dict = {
        "items": List[CartItem]
    }